<template>
    <div class="todo_footer" v-show="total">
        <label>
            <input type="checkbox" :checked="isAllChecked" @change="checkAll">
        </label>
        已完成{{doneNum}}个 / 全部 {{total}} 个
        <button @click="clearAll">清除所有已完成</button>
    </div>
</template>

<script>
export default {
    name: 'TodoFooter',
    props: ['todos', "clearAllDone"],
    methods: {
        checkAll(e) {
            console.log('isChecked: ',e.target.checked);
            if(e.target.checked) {
                this.todos.forEach(todo => todo.isDone = true);
            } else {
                this.todos.forEach(todo => todo.isDone = false);
            }
        },
        clearAll() {
            if(confirm('你确定清除全部任务吗？')) {
                this.clearAllDone();
            }
        }
    },
    computed: {
        total() {
            return this.todos.length;
        },
        doneNum() {
            return this.todos.filter(todo => todo.isDone).length;
        },
        isAllChecked() {
            return this.total === this.doneNum && this.total > 0;
        }
    }
}
</script>

<style scoped>
.todo_footer {
    padding-left: 10px;
    box-sizing: border-box;
}
button {
    background-color: coral;
    outline: none;
    border: none;
    border-radius: 4px;
    padding: 5px 10px;
    box-sizing: border-box;
    cursor: pointer;
    color: #fff;
    float: right;
    margin-right: 5px;
}
</style>